const FarmerProductList = require('../models/farmerProductList');

exports.createFarmerProductList = async (req, res) => {
  try {
    const { farmer_id, village_id, is_listed ,name} = req.body;
    const id = await FarmerProductList.create(farmer_id, village_id, is_listed,name);
    res.status(201).json({ id, farmer_id, village_id, is_listed ,name});
  } catch (error) {
    res.status(400).json({ message: error.message });
  }
};

exports.getFarmerProductListByFarmerId = async (req, res) => {
  try {
    const rows = await FarmerProductList.getByFarmerId(req.params.farmerId);
    res.json(rows);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};

exports.updateFarmerProductListIsListed = async (req, res) => {
  try {
    const { is_listed } = req.body;
    const affectedRows = await FarmerProductList.updateIsListed(req.params.id, is_listed);
    if (affectedRows === 0) {
      return res.status(404).json({ message: '记录不存在' });
    }
    res.json({ message: '更新成功' });
  } catch (error) {
    res.status(400).json({ message: error.message });
  }
};